package wjl.project.dubboelabauthprovider.Service;

import com.alibaba.fastjson.JSON;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import wjl.project.dubboelabauthapi.ResultUtil;
import wjl.project.dubboelabauthapi.UserQo;
import wjl.project.dubboelabauthapi.UserService;
import wjl.project.dubboelabauthapi.UserVo;
import wjl.project.dubboelabauthprovider.Entity.EUser;
import wjl.project.dubboelabauthprovider.Repository.EUserRepository;

import javax.annotation.Resource;

/**
 * UserServiceImpl
 *
 * @blame Android Team
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    EUserRepository eUserRepository;

    @Override
    public ResultUtil.Result getUserInfoByPhone(String phone) {
        if ("".equals(phone)||phone==null){
            return ResultUtil.REGISTER_PHONE_INVALID;
        }
        try {
            EUser eUser = eUserRepository.findByMobilePhone(phone).orElse(null);
            if (eUser == null){
                return ResultUtil.SEARCH_USER_INVALID;
            }
            UserVo userVo = UserVo.builder().mail(eUser.getMail()).phone(eUser.getMobilePhone())
                    .userId(eUser.getUserId()).username(eUser.getUsername()).build();
            return ResultUtil.Result.builder().code(ResultUtil.SUCCESS_CODE).message(JSON.toJSONString(userVo)).build();
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.SEARCH_INVALID;
        }

    }

    @Override
    public ResultUtil.Result register(Object registerObject) {
        try {
            EUser eUser = (EUser)registerObject;
            if (eUser.getUserId()!=0L){
                return ResultUtil.REGISTER_ID_INVALID;
            }
            if ("".equals(eUser.getMobilePhone())||eUser.getMobilePhone()==null){
                return ResultUtil.REGISTER_PHONE_INVALID;
            }else if ("".equals(eUser.getMail())||eUser.getMail()==null){
                return ResultUtil.REGISTER_MAIL_INVALID;
            } else if ("".equals(eUser.getPassword())||eUser.getPassword()==null){
                return ResultUtil.REGISTER_PASSWORD_INVALID;
            }else {
                EUser e = eUserRepository.findByMobilePhoneAndMail(eUser.getMobilePhone(),eUser.getMail()).orElse(null);
                if (e!=null){
                    if (e.getMail().equals(eUser.getMail())){
                        return  ResultUtil.REGISTER_MAIL_EXIST;
                    }else {
                        return ResultUtil.REGISTER_PHONE_EXIST;
                    }
                }else {
                    String securityPassword =  new BCryptPasswordEncoder().encode(eUser.getPassword());
                    eUser.setPassword(securityPassword);
                    eUserRepository.save(eUser);
                }
                return ResultUtil.REGISTER_VALID;
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.REGISTER_SERVER_ERROR;
        }
    }
}
